package xyz.yuzegod.megawalls.nms;

import net.minecraft.server.v1_8_R3.*;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.plugin.Plugin;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.List;

public class CustomBat extends EntityBat implements CustomEntity, Runnable
{
    private GamePlayer gamePlayer;
    private int taskId;
    
    public CustomBat(final World world) {
        super(world);
        this.taskId = 0;
        this.taskId = Bukkit.getScheduler().runTaskTimer((Plugin)MegaWalls.getInstance(), (Runnable)this, 0L, 5L).getTaskId();
    }
    
    public GamePlayer getGamePlayer() {
        if (this.gamePlayer != null) {
            return this.gamePlayer;
        }
        final String name = ChatColor.stripColor(this.getCustomName());
        final Player player = Bukkit.getPlayer(name);
        if (player == null) {
            return null;
        }
        return this.gamePlayer = GamePlayer.get(player.getUniqueId());
    }
    
    public void setGamePlayer(final GamePlayer gamePlayer) {
        this.gamePlayer = gamePlayer;
        this.setCustomName(gamePlayer.getDisplayName(null));
        this.setCustomNameVisible(true);
    }
    
    public void setGoalTarget(final EntityLiving entityliving) {
        this.setGoalTarget(entityliving, EntityTargetEvent.TargetReason.CLOSEST_PLAYER, false);
    }
    
    public void setGoalTarget(final EntityLiving entityliving, final EntityTargetEvent.TargetReason reason, final boolean fire) {
        if (!(entityliving instanceof EntityPlayer)) {
            return;
        }
        final GamePlayer gamePlayer = this.getGamePlayer();
        this.follow(gamePlayer.getPlayer());
        if (gamePlayer.getGameTeam() != null && gamePlayer.getGameTeam().isInTeam(GamePlayer.get(((EntityPlayer)entityliving).getBukkitEntity().getUniqueId()))) {
            return;
        }
        super.setGoalTarget(entityliving, reason, fire);
    }
    
    public void die() {
        Bukkit.getScheduler().cancelTask(this.taskId);
        super.die();
    }
    
    public void follow(final Player player) {
        if (player == null) {
            return;
        }
        this.getNavigation().a(2.0f);
        final Location targetLocation = player.getLocation();
        final PathEntity path = this.getNavigation().a(targetLocation.getX() + 1.0, targetLocation.getY(), targetLocation.getZ() + 1.0);
        try {
            final int distance = (int)Bukkit.getPlayer(player.getName()).getLocation().distance(this.getBukkitEntity().getLocation());
            if (distance > 10 && this.valid && player.isOnGround()) {
                this.setLocation((double)targetLocation.getBlockX(), (double)targetLocation.getBlockY(), (double)targetLocation.getBlockZ(), 0.0f, 0.0f);
            }
            if (path != null && distance > 3.3) {
                final double speed = 1.05;
                this.getNavigation().a(path, speed);
                this.getNavigation().a(speed);
            }
        }
        catch (IllegalArgumentException e) {
            this.setLocation((double)targetLocation.getBlockX(), (double)targetLocation.getBlockY(), (double)targetLocation.getBlockZ(), 0.0f, 0.0f);
        }
    }
    
    public void run() {
        for (final Player player : PlayerUtils.getNearbyPlayers(this.getBukkitEntity().getLocation(), 10.0)) {
            if (player == null) {
                continue;
            }
            final GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
            if (gamePlayer == null || gamePlayer.isSpectator()) {
                continue;
            }
            if (this.getGamePlayer().getGameTeam().isInTeam(gamePlayer)) {
                continue;
            }
            this.setGoalTarget((EntityLiving)((CraftPlayer)player).getHandle());
        }
        final EntityLiving target = this.getGoalTarget();
        if (target != null && target.getBukkitEntity().getLocation().distance(this.getBukkitEntity().getLocation()) <= 3.0) {
            ParticleEffect.EXPLOSION_HUGE.display(0.0f, 0.0f, 0.0f, 1.0f, 1, this.getBukkitEntity().getLocation().add(0.0, 1.0, 0.0), 30.0);
            for (final Player player2 : this.getNearbyPlayers(this.getBukkitEntity().getLocation(), this.gamePlayer.getPlayer(), 3)) {
                player2.damage(2.0, (Entity)this.gamePlayer.getPlayer());
            }
            this.die();
        }
    }
    
    private List<Player> getNearbyPlayers(final Location location, final Player player, final int radius) {
        final List<Player> players = new ArrayList<Player>();
        final GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
        for (final Player other : PlayerUtils.getNearbyPlayers(location, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator() && !gamePlayer.getGameTeam().isInTeam(gameOther)) {
                if (other.getLocation().distance(location) > radius) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
    
    protected Item getLoot() {
        return null;
    }
}
